<template>
    <div class="main">
        <div class="title">
            <a class="active" href="/login">登录</a>
            <span>·</span>
            <a href="/register">注册</a>
        </div>

        <div class="sign-up-container">
            <el-form ref="userForm" :model="user" :rules="rules">
                <el-link
                    v-if="actionType!==2"
                    icon="el-icon-cpu"
                    @click.prevent="actionType=2"
                >忘记密码</el-link>
                <span v-if="actionType!==2">
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                </span>

                <el-link v-if="actionType!==1" @click.prevent="actionType=1">
                    验证码登录
                    <i class="el-icon-message el-icon--right"></i>
                </el-link>
                <span v-if="actionType===2">
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                </span>
                <el-link v-if="actionType!==0" @click.prevent="actionType=0">
                    密码登录
                    <i class="el-icon-key el-icon--right"></i>
                </el-link>

                <el-form-item
                    class="input-prepend restyle"
                    prop="mobile"
                    :rules="[{ required: true, message: '请输入手机号码', trigger: 'blur' },{validator: checkPhone, trigger: 'blur'}]"
                >
                    <div>
                        <el-input type="text" placeholder="手机号" v-model="user.mobile" />
                        <i class="iconfont icon-phone" />
                    </div>
                </el-form-item>

                <!--忘记密码的两个输入框-->
                <el-form-item
                    class="input-prepend restyle no-radius"
                    prop="password1"
                    v-if="actionType===2"
                >
                    <div>
                        <el-input type="password" placeholder="设置新密码" v-model="user.password1" />
                        <i class="iconfont icon-password" />
                    </div>
                </el-form-item>
                <el-form-item
                    class="input-prepend restyle no-radius"
                    prop="password2"
                    v-if="actionType===2"
                >
                    <div>
                        <el-input type="password" placeholder="确认新密码" v-model="user.password2" />
                        <i class="iconfont icon-password" />
                    </div>
                </el-form-item>

                <!--/忘记密码的两个输入框-->

                <!--验证码-->
                <el-form-item
                    class="input-prepend"
                    prop="code"
                    :rules="[{ required: true, message: '请输入验证码', trigger: 'blur' }]"
                    v-if="actionType!==0"
                >
                    <div style="width: 100%;display: block;float: left;position: relative">
                        <el-input
                            type="text"
                            placeholder="验证码"
                            v-model="user.code"
                            @change="submitLogin()"
                        />
                        <i class="iconfont icon-phone" />
                    </div>
                    <div class="btn" style="position:absolute;right: 0;top: 6px;width: 40%;">
                        <a
                            href="javascript:"
                            type="button"
                            :value="codeTest"
                            @click="getCodeFun()"
                            style="border: none;background-color: none"
                        >{{codeTest}}</a>
                    </div>
                </el-form-item>
                <!--/验证码-->
                <el-form-item
                    class="input-prepend"
                    prop="password"
                    :rules="[{ required: true, message: '请输入密码', trigger: 'blur' }]"
                    v-if="actionType===0"
                >
                    <div>
                        <el-input
                            type="password"
                            placeholder="密码"
                            v-model="user.password"
                            @change="submitLogin()"
                        />
                        <i class="iconfont icon-password" />
                    </div>
                </el-form-item>

                <div class="btn">
                    <input
                        type="button"
                        class="sign-in-button"
                        :value="text"
                        @click="submitLogin()"
                    />
                </div>
            </el-form>
            <!-- 更多登录方式 -->
            <div class="more-sign">
                <h6>社交帐号登录</h6>
                <ul>
                    <li>
                        <a
                            id="weixin"
                            class="weixin"
                            target="_blank"
                            href="http://localhost:8160/ucenter/front/wxapi/login"
                        >
                            <i class="iconfont icon-weixin" />
                        </a>
                    </li>
                    <li>
                        <a id="qq" class="qq" target="_blank" href="#">
                            <i class="iconfont icon-qq" />
                        </a>
                    </li>
                </ul>
            </div>
        </div>
    </div>
</template>

<script>
import "~/assets/css/sign.css";
import "~/assets/css/iconfont.css";
import cookie from "js-cookie";
import { sendMsm } from "@/api/ucenter/msm";
import {
    loginByPhone,
    getLoginInfo,
    loginByCode,
    updatePassword,
} from "@/api/ucenter/user";
export default {
    layout: "sign",

    data() {
        var validatePass = (rule, value, callback) => {
            if (value === "") {
                callback(new Error("请输入密码"));
            } else {
                if (this.user.password2 !== "") {
                    this.$refs.userForm.validateField("checkPass");
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === "") {
                callback(new Error("请再次输入密码"));
            } else if (value !== this.user.password1) {
                callback(new Error("两次输入密码不一致!"));
            } else {
                callback();
            }
        };
        return {
            user: {
                mobile: "",
                password: "",
                code: "",
                password1: "", //新密码
                password2: "", //旧密码
            },
            loginInfo: {},
            sending: true, //是否发送验证码
            second: 120, //倒计时间
            codeTest: "获取验证码",
            actionType: 0, //0 密码登录  1 验证码登录  2 修改密码
            text: "登录",
            rules: {
                password1: [{ validator: validatePass, trigger: "blur" }],
                password2: [{ validator: validatePass2, trigger: "blur" }],
            },
        };
    },

    watch: {
        actionType(newValue, oldValue) {
            if (newValue === 2) {
                this.text = "提交";
            } else {
                this.text = "登录";
            }
        },
    },

    methods: {
        submitLogin() {
            this.$refs.userForm.validate((valid) => {
                if (valid) {
                    if (this.actionType === 0) {
                        loginByPhone(this.user).then((response) => {
                            this.$message({
                                type: "success",
                                message: "登录成功",
                            });
                            //把token存在cookie中、也可以放在localStorage中
                            cookie.set("edu_token", response.data.token, {
                                domain: "localhost",
                            });
                            //登录成功根据token获取用户信息
                            getLoginInfo().then((response) => {
                                this.loginInfo = response.data.user;
                                //将用户信息记录cookie
                                cookie.set("edu_user", this.loginInfo, {
                                    domain: "localhost",
                                });
                                //跳转页面
                                window.location.href = "/";
                            });
                        });
                    } else if (this.actionType === 1) {
                        loginByCode(this.user).then((response) => {
                            this.$message({
                                type: "success",
                                message: "登录成功",
                            });
                            //把token存在cookie中、也可以放在localStorage中
                            cookie.set("edu_token", response.data.token, {
                                domain: "localhost",
                            });
                            //登录成功根据token获取用户信息
                            getLoginInfo().then((response) => {
                                this.loginInfo = response.data.user;
                                //将用户信息记录cookie
                                cookie.set("edu_user", this.loginInfo, {
                                    domain: "localhost",
                                });
                                //跳转页面
                                window.location.href = "/";
                            });
                        });
                    } else {
                        //修改密码
                        updatePassword(
                            this.user.mobile,
                            this.user.password1,
                            this.user.code
                        ).then((response) => {
                            this.actionType = 0;
                            let m = this.user.mobile;
                            this.user = { mobile: m };
                            this.$message({
                                message: "成功",
                                type: "success",
                            });
                        });
                    }
                } else {
                    return false;
                }
            });
        },

        checkPhone(rule, value, callback) {
            //debugger
            if (!/^1[3456789]\d{9}$/.test(value)) {
                return callback(new Error("手机号码格式不正确"));
            }
            return callback();
        },

        getCodeFun() {
            //sending = false
            //his.sending原为true,请求成功，!this.sending == true，主要是防止有人把disabled属性去掉，多次点击；
            if (!this.sending) return;

            //debugger
            // prop 换成你想监听的prop字段
            this.$refs.userForm.validateField("mobile", (errMsg) => {
                if (errMsg == "") {
                    this.timeDown();
                    sendMsm(this.user.mobile).then((res) => {
                        this.sending = false;
                    });
                }
            });
        },

        timeDown() {
            let result = setInterval(() => {
                --this.second;
                this.codeTest = this.second;
                if (this.second < 1) {
                    clearInterval(result);
                    this.sending = true;
                    //this.disabled = false;
                    this.second = 120;
                    this.codeTest = "获取验证码";
                }
            }, 1000);
        },
    },
};
</script>
<style>
.el-form-item__error {
    z-index: 9999999;
}
</style>